package com.lwc.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Configuration
public class Rabbitconfig {

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadqueue(){
return QueueBuilder.durable("dead-queue").build();
    }

//@Bean
   /* public Queue queue(){
    Map<String, Object>map=new HashMap<>();
    //设置死信相关器
    map.put("x-dead-letter-exchange","dead-queue");//绑定死信路由器
    map.put("x-dead-letter-routing-key","dead");//绑定死信路由键
    map.put("x-message-ttl",10000); //设置过期时间
        return  new Queue("normal",true,false,false,map);
    }*/

    /**
     * 正常的队列
     * @return
     */
    @Bean
    public Queue queue(){
        return QueueBuilder.durable("queue")
                .deadLetterRoutingKey("dead")//绑定死信路由键
                .deadLetterExchange("dead-ex")//死信交换器
                .ttl(10000)
                .build();
    }

    /**
     * 声明正常交换器
     */
    @Bean
    public DirectExchange directExchange(){
        return  new DirectExchange("normal-ex");
    }
    /**
     * 声明死信交换器
     */
    @Bean
    public DirectExchange deadExchange(){
        return  new DirectExchange("dead-ex");
    }

    //绑定正常队列和正常交换器
    @Bean
    public Binding binding(DirectExchange directExchange,Queue queue){
return BindingBuilder.bind(queue).to(directExchange).with("normal");
    }

    //绑定死信队列和死信交换器
    @Bean
    public Binding deadbinding(DirectExchange deadExchange,Queue deadqueue){
        return BindingBuilder.bind(deadqueue).to(deadExchange).with("dead");
    }

}
